package com.jbtm.mall.util;

import org.springframework.util.Base64Utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.security.MessageDigest;
import java.security.SecureRandom;

/**
 *  加密解密相关的工具类
 * @author zhaowh
 * @createdate 03/29/2017
 */
public class SecurityUtil {
    /**
     *  Base64 加密字符串
     * @param data 待加密的数据
     * @return
     */
    public static String base64Encrypt(byte[] data) {
        try{
            return new String(Base64Utils.encode(data));
        }catch(Exception e){
            return null;
        }
    }
    /**
     *  Base64 解密字符串
     * @param str 待解密的字符串
     * @return
     */
    public static byte[] base64Decrypt(String str) {
        try{
            return Base64Utils.decode(str.getBytes());
        }catch(Exception e){
            return null;
        }
    }

    /**
     * DES3加密
     * @param key 密钥
     * @param str 加密的字符串
     * @return
     * @throws Exception
     */
    public static byte[] des3Encrypt(String key, String str){
        try {
            byte[] newkey = key.getBytes();
            SecureRandom sr = new SecureRandom();
            DESedeKeySpec dks = new DESedeKeySpec(newkey);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey securekey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
            byte[] bt = cipher.doFinal(str.getBytes("utf-8"));
            return bt;
        }catch (Exception e){
            return null;
        }
    }

    /**
     * DES3 解密
     * @param key 密钥
     * @param data 数据
     * @return
     */
    public static String des3Decrypt(String key, byte[] data){
        String resutl = "";
        try{
            byte[] newkey = key.getBytes();
            DESedeKeySpec dks = new DESedeKeySpec(newkey);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey securekey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, securekey, new SecureRandom());
            byte[] bb = cipher.doFinal(data);
            resutl = new String(bb, "UTF-8");
        }catch (Exception e){}
        return resutl;
    }

    /**
     * MD5 加密
     * @param data 要加密的字符串
     * @return
     */
    public static String md5(String data) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F' };
        try {
            byte[] btInput = data.getBytes("UTF-8");
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
